"""
复合型序列(趋势、季节、周期和随机成分)：Holt
"""
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import TimeSeriesSplit
from statsmodels.tsa.api import Holt
# from statsmodels.tsa.holtwinters import Holt


def holt_trend_season(series, alpha):
    """
    复合型序列(趋势、季节、周期和随机成分)：Holt
    :param series:
    :param alpha:
    :return:
    """
    if isinstance(series, pd.Series):
        data_set = series.to_numpy()

    if not isinstance(series, np.ndarray):
        data_set = np.array(series)
    else:
        data_set = series

    rms_holt = []

    tscv = TimeSeriesSplit()
    for train_idx, test_idx in tscv.split(data_set):
        train, test = data_set[train_idx], data_set[test_idx]
        peroid = len(test_idx)

        # holt
        """
        smoothing_level：简单指数平滑的alpha值
        smoothing_slope：holt趋势的beta值
        
        https://www.statsmodels.org/stable/generated/statsmodels.tsa.
        holtwinters.Holt.fit.html#statsmodels.tsa.holtwinters.Holt.fit
        """
        holt_model = Holt(data_set).fit(smoothing_level=alpha,
                                        smoothing_slope=0.1)
        y_pred = holt_model.forecast(peroid)
        rms_holt.append(np.sqrt(mean_squared_error(test, y_pred)))

    print('rms: {}'.format(np.mean(rms_holt)))
